package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import androidx.lifecycle.lifecycleScope
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.Td2Adapter
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.databinding.ActivityTdBinding
import client.comm.baoding.ui.vm.TdViewModel
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.widget.EmptyLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class TdActivity : BaseMiddleActivity<TdViewModel, ActivityTdBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_td
    }

    val adapter: Td2Adapter by lazy {
        Td2Adapter(this, mImgLoader).apply {
            level = intent.getIntExtra("level", 1) + 1

            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
            }
        }
    }

    override fun initView() {
        binding.event = this
        binding.recyclerView.adapter = adapter.withLoadStateFooter(PagerFooterAdapter())
//        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))
        binding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }

    }

    override fun initData() {
        intent.getStringExtra("bean").apply {
            viewModel.bean.value = this
            lifecycleScope.launch {
                viewModel.pagingDataAuto(20, hold = {
                    var ret = viewModel.server.get_user_team(viewModel.bean.value!!, it).body()

                    PagingJsonResult(
                        list = ret?.data?.sub_list,
                        code = ret?.code,
                        message = ret?.message
                    )
                }).collect {
                    adapter.submitData(it)
                }
            }
        }
        intent.getStringExtra("name").let {
            binding.title = "${it}的团队"
        }
    }

    override fun initViewModel(): TdViewModel = createViewModel()

    /**
     * 奖励记录
     */
    fun jljl() {
        startActivity(Intent(this, TdJlJlActivity::class.java))
    }

}